package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.ShopTagComponent;
import com.dy.lock.screen.constants.Constant;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.ShopTagInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.ShopTagService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.ShopTagVo;
import com.google.gson.Gson;

@Component("shopTagComponent")
public class ShopTagComponentImpl implements ShopTagComponent {
	private static final Logger log = Logger
			.getLogger(ShopTagComponentImpl.class);
	private Gson gson;

	@Resource(name = "shopTagService")
	private ShopTagService shopTagService;

	public ShopTagComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<ShopTagInfo> list(String body, Head head) {
		ShopTagVo vo = gson.fromJson(body, ShopTagVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 9;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("isDelete", Constant.IS_NOT_DELETE);
			List<ShopTagInfo> tagList = this.shopTagService.queryPage(paraMap);
			if (tagList != null && !tagList.isEmpty()) {
				return tagList;
			} else {
				return new ArrayList<ShopTagInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",获取标签列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
